package com.example.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {
	Paint paint;
	
	int measureWidth(int measureSpec){
		return View.resolveSize(480, measureSpec);
	}
	int measureHeight(int measureSpec) {
		int result = 50;//默认大小
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode) {
		case MeasureSpec.EXACTLY:
			result = specSize;
			break;
		case MeasureSpec.AT_MOST:
			result = Math.min(result, specSize);
			break;
		case MeasureSpec.UNSPECIFIED:
			//do nothing
			break;
		default:
			break;
		}
        return result;
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		widthMeasureSpec = measureWidth(widthMeasureSpec);
		heightMeasureSpec = measureHeight(heightMeasureSpec);
		super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawText("custom view", 0, 24, paint);
	}
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
	}
	public CustomView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
	}
}
